//
//  AccountPasswordLoginViewController.m
//  HeartWorldClient
//
//  Created by 董知樾 on 2017/7/13.
//  Copyright © 2017年 谭明权. All rights reserved.
//  用户登录失败提示重新输入和推荐使用验证码登录，用户登录失败三次后，提示更换登录方式、找回密码

#import "AccountPasswordLoginViewController.h"
#import "YCGetBackPasswordViewController.h"
#import "YCLoginManager.h"

@interface AccountPasswordLoginViewController ()

@property (weak, nonatomic) IBOutlet UILabel *phoneNumberLabel;
@property (weak, nonatomic) IBOutlet UITextField *passwordTextField;
@property (weak, nonatomic) IBOutlet UIButton *loginButton;

@property (nonatomic, assign) NSInteger loginFailureCount;

@end

@implementation AccountPasswordLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"登录HMO管家";
    
    _loginButton.backgroundColor = [UIColor colorWithCommonType:commonColorBlue];
    _loginButton.layer.cornerRadius = 5;
    _loginButton.layer.masksToBounds = YES;
    
    _loginFailureCount = 0;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    _phoneNumberLabel.text = [NSString stringWithFormat:@"手机号：%@", [[YCLoginManager shareManager].phoneNumber telephoneNumberFormat]];
}

- (IBAction)loginBtnClick:(UIButton *)sender {
    if (_loginFailureCount >= 3) {
        [self showTip];
        return;
    }
    
    if (_passwordTextField.text.length < 6) {
        [self presentFailureTips:@"密码不小于六位"];
        return;
    }
    
    [self presentLoadingTips:nil];
    [[YCLoginManager shareManager] accountPasswordLoginWithPassword:_passwordTextField.text success:^(NSString *memberId) {
        [self dismissTips];
        [[YCLoginManager shareManager] loginSuccessConfigMemberId:memberId];
        [self.navigationController popToRootViewControllerAnimated:YES];
        
    } failure:^(NSString *message) {
        [self presentFailureTips:message];
        _passwordTextField.text = nil;
        _loginFailureCount ++;
        
    }];
    
}

- (IBAction)useVerifyLogin:(UIButton *)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)forgetThePassword:(UIButton *)sender {
    YCGetBackPasswordViewController *vc = [[YCGetBackPasswordViewController alloc] init];
    [self showViewController:vc sender:nil];
}

- (void)showTip {
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"账号密码总输错？" message:@"（试试其他登录方式）" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *action0 = [UIAlertAction actionWithTitle:@"手机号快捷登录（推荐）" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self.navigationController popViewControllerAnimated:YES];
    }];
    [alert addAction:action0];
    [alert addAction:[UIAlertAction actionWithTitle:@"找回密码" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        YCGetBackPasswordViewController *vc = [[YCGetBackPasswordViewController alloc] init];
        [self showViewController:vc sender:nil];

    }]];
    [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }]];
    alert.preferredAction = action0;
    [self showDetailViewController:alert sender:nil];
}

- (void)dealloc {
    [self clearKeyboardCorverNotificationAndGesture];
    [self tapGestureHandel];
}

@end
